<template>
    <div>
        <div class="top-banner-back">
            <div>
                当前校区：
                <el-select placeholder="请选择" v-model="schoolId" class="width240" @change="findShopsListChange">
                     <el-option v-for="(item,index) in findShopsList" :key="index" :label="item.schoolName" :value="item.id"></el-option>
                </el-select>
                 <el-input class="width240 marleft20" placeholder="请输入活动名称" v-model="enrollName"  @keyup.enter.native="handleIconClick">
                     <i slot="suffix" class="el-input__icon el-icon-search pointer" @click="handleIconClick"></i>
                 </el-input>
            </div>
            <div class="martop15">
                <el-button type="primary" @click="toAddRule(schoolId,'')">添加规则</el-button>
            </div>
        </div>
        <div class="edu-table-lr">
            <el-table :data="tableData">
                <el-table-column prop="sort" label="参与活动">
                    <template slot-scope="scope">
                       <ul>
                           <li v-for="(item,index) in scope.row.enrollList" :key="index">{{item.enrollName}}</li>
                       </ul>
                    </template>
                </el-table-column>
                <el-table-column label="使用推荐码">
                    <template slot-scope="scope">
                       减免学费{{scope.row.reductionMoney}}元
                       
                    </template>
                </el-table-column>
                <el-table-column label="推荐人奖励">
                    <template slot-scope="scope">
                        <div v-if="scope.row.giveCash">{{scope.row.giveCash}}现金</div>
                        <div v-if="scope.row.giveFans">{{scope.row.giveFans}}粉币</div>
                        <div v-if="scope.row.giveIntegral">{{scope.row.giveIntegral}}积分</div>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" plain size="mini" @click="toAddRule(schoolId,scope.row.id)">编辑</el-button>
                        <el-button type="info" plain size="mini" @click="deleteList(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="edu-pagination">
             <el-pagination background v-if="pageCount > 1"
             @size-change="handleSizeChange" 
             @current-change="handleCurrentChange" 
             :current-page.sync="currentPage"
            :page-size="pageSize"
            layout="prev, pager, next, jumper"
            :total="rowCount">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import common from '@/assets/js/common'
export default {
  data(){
      return{
          tableData:[],
          xiaoqu:'',
          currentPage:1,
          findShopsList:[],
          schoolId:'',
          curpage:1,
          pageCount:1,
          pageSize:10,  
          rowCount:10,  
          enrollName:''
      }
  },
  methods: {
      //分页查询教室数据
        loadData(curpage,schoolId,enrollName){
            let that = this
            common.ajax({
                'url':eduAPI.activeAPI.findSetRecommendList,
                'type': "GET",
                'data':{
                    curPage: curpage,
                    schoolId: schoolId,
                    enrollName:enrollName
                },
                'success': function(data) {
                    console.log(data,'推荐分页查询数据');
                    if (data.data) {
                        that.tableData = data.data.subList
                        that.pageSize = data.data.pageSize
                        that.pageCount = data.data.pageCount
                        that.rowCount = data.data.rowCount
                    }else{
                        that.tableData = []
                        that.pageCount = ''
                        that.rowCount = ''
                    }
                }
            });
        },

        //校区切换列表
        findShopsListChange(val){
            this.schoolId = val
            this.loadData(1,this.schoolId,this.enrollName)
        },

        //handleIconClick  搜索
        handleIconClick(){
            this.loadData(1,this.schoolId,this.enrollName)
        },

        //添加规则 编辑
        toAddRule(schoolId,id){
            this.$router.push({name:'recomSettingsAddRule',params:{schoolId:schoolId,id:id}})
        },

        //删除
        deleteList(index){
            let that = this
            parent.window.postMessage("openMask()", "*");
            this.$confirm('确定要删除该规则吗？删除后不可恢复?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                common.ajax({
                    'url': eduAPI.activeAPI.delSetRecommend,
                    'type': "POST",
                    'data':{
                        setRecommendIds: index,
                        schoolId: that.schoolId
                    },
                    'success': function(data) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        parent.window.postMessage("closeMask()", "*");
                        that.loadData(that.curpage,that.schoolId,that.enrollName)
                    }
                });
                
            }).catch(() => {
                that.$message({
                    type: 'info',
                    message: '已取消删除'
                });      
                parent.window.postMessage("closeMask()", "*");    
            });
        },

        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            let that = this
            that.curpage = val
            that.loadData(val,that.schoolId,that.enrollName)
        }
  },
   beforeMount(){    
        let that = this
        let router =  that.$route.query
        //查询校区
        common.ajax({
            'url':eduAPI.activeAPI.findAllSchoolList,
            'type': "GET",
            'success': function(data) {
                console.log(data,'查询校区数据');
                if (data.data) {
                    that.findShopsList = data.data
                    if(router.id){
                        that.schoolId = parseInt(router.id)
                    }else{
                        that.schoolId = data.data[0].id
                    }
                    that.loadData(that.curpage,that.schoolId,that.enrollName)
                }
            }
        });
    },
}
</script>
